﻿#include "Connect_C2S.h"
#include <string.h>
#include "Connect.h"

static uint32_t nextHandle = 1;
uint32_t Connect_C2S_CreateHandle()
{
    return nextHandle++ & 0x00ffffff;
}

// 通用打包函数
static int Connect_C2S_Pack(uint8_t* connectPayload, uint8_t cmd, uint32_t handle, const void* data, uint16_t dataSize)
{
    SetPayloadStart(connectPayload);
    SetPayloadSize(connectPayload, dataSize);
    SetPayloadCmd(connectPayload, cmd);
    SetPayloadHandle(connectPayload, handle);
    // CRC可选，暂不设置
    // SetPayloadCrc16(connectPayload, 0);
    if (dataSize > 0 && data != NULL)
        memcpy(connectPayload + 10, data, dataSize);
    return 10 + dataSize;
}

int Connect_C2S_TcpConnect(uint8_t* connectPayload, uint32_t handle, const char* host, uint16_t port)
{
    size_t hostLen = strlen(host) + 1;
    uint8_t buf[256];
    int offset = 0;
    memcpy(buf + offset, host, hostLen);
    offset += hostLen;
    memcpy(buf + offset, &port, sizeof(port));
    offset += sizeof(port);
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_TcpConnect, handle, buf, offset);
}

int Connect_C2S_TcpDisconnect(uint8_t* connectPayload, uint32_t handle)
{
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_TcpDisconnect, handle, NULL, 0);
}

int Connect_C2S_TcpSend(uint8_t* connectPayload, uint32_t handle, const uint8_t* data, uint16_t size)
{
    uint8_t buf[2 + 1024];
    int offset = 0;
    memcpy(buf + offset, &size, sizeof(size));
    offset += sizeof(size);
    memcpy(buf + offset, data, size);
    offset += size;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_TcpSend, handle, buf, offset);
}

int Connect_C2S_TcpList(uint8_t* connectPayload, uint32_t handle)
{
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_TcpList, handle, NULL, 0);
}

int Connect_C2S_SavedataList(uint8_t* connectPayload, uint32_t handle)
{
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_SavedataList, handle, NULL, 0);
}

int Connect_C2S_SavedataSave(uint8_t* connectPayload, uint32_t handle, const char* name, const uint8_t* data, uint16_t size)
{
    size_t nameLen = strlen(name) + 1;
    uint8_t buf[256 + 2 + 1024];
    int offset = 0;
    memcpy(buf + offset, name, nameLen);
    offset += nameLen;
    memcpy(buf + offset, &size, sizeof(size));
    offset += sizeof(size);
    memcpy(buf + offset, data, size);
    offset += size;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_SavedataSave, handle, buf, offset);
}

int Connect_C2S_SavedataLoad(uint8_t* connectPayload, uint32_t handle, const char* name, uint8_t* data, uint16_t maxSize)
{
    size_t nameLen = strlen(name) + 1;
    uint8_t buf[256 + 2];
    int offset = 0;
    memcpy(buf + offset, name, nameLen);
    offset += nameLen;
    memcpy(buf + offset, &maxSize, sizeof(maxSize));
    offset += sizeof(maxSize);
    // data为输出，不打包
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_SavedataLoad, handle, buf, offset);
}

int Connect_C2S_SavedataDelete(uint8_t* connectPayload, uint32_t handle, const char* name)
{
    size_t nameLen = strlen(name) + 1;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_SavedataDelete, handle, name, nameLen);
}

int Connect_C2S_NodeRegist(uint8_t* connectPayload, uint32_t handle, const char* nodeName)
{
    size_t nameLen = strlen(nodeName) + 1;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_NodeRegist, handle, nodeName, nameLen);
}

int Connect_C2S_NodeUnregist(uint8_t* connectPayload, uint32_t handle, const char* nodeName)
{
    size_t nameLen = strlen(nodeName) + 1;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_NodeUnregist, handle, nodeName, nameLen);
}

int Connect_C2S_NodePublish(uint8_t* connectPayload, uint32_t handle, const char* nodeName, const uint8_t* data, uint16_t size)
{
    size_t nameLen = strlen(nodeName) + 1;
    uint8_t buf[256 + 2 + 1024];
    int offset = 0;
    memcpy(buf + offset, nodeName, nameLen);
    offset += nameLen;
    memcpy(buf + offset, &size, sizeof(size));
    offset += sizeof(size);
    memcpy(buf + offset, data, size);
    offset += size;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_NodePublish, handle, buf, offset);
}

int Connect_C2S_NodeSubscribe(uint8_t* connectPayload, uint32_t handle, const char* nodeName)
{
    size_t nameLen = strlen(nodeName) + 1;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_NodeSubscribe, handle, nodeName, nameLen);
}

int Connect_C2S_OtaRecived(uint8_t* connectPayload, uint32_t handle, const uint8_t* data, uint16_t size)
{
    uint8_t buf[2 + 1024];
    int offset = 0;
    memcpy(buf + offset, &size, sizeof(size));
    offset += sizeof(size);
    memcpy(buf + offset, data, size);
    offset += size;
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_OtaRecived, handle, buf, offset);
}

int Connect_C2S_OtaFinshed(uint8_t* connectPayload, uint32_t handle)
{
    return Connect_C2S_Pack(connectPayload, ConnectCmd_C2S_OtaFinshed, handle, NULL, 0);
}
